$KCODE = 'u'

class Array
  def every(&block)
    arity = block.arity
    return self.each(&block) if arity <= 0

    i = 0
    while i < self.size
      yield(*self[i, arity])
      i += arity
    end
    self
  end
end

class WordSet
  def self.all_patterns_of(entry)
    list = Array.new
    entry.split(/（|）|\(|\)/).every do |word,postfix|
      unless postfix.nil?
        list << [word,word+postfix]
      else
        list << [word]
      end
    end
    self.expand(list)
  end
  
  protected
  def self.expand(list)
    if list.size > 1
      patterns = Array.new
      self.expand(list[0..-2]).each do |pre|
        list.last.each do |post|
          patterns << pre + post
        end
      end
    else
      patterns = list.last
    end
    patterns
  end
end
